// ignore_for_file: sort_child_properties_last, camel_case_types, must_be_immutable

import 'package:flutter/material.dart';

import '../../tool/assets.dart';

enum kButtonStyle { text, icon, custom }

///自定义基类button
class kButton extends StatefulWidget {
  late final String name;
  late final Color textColor;
  late final Color groundColor;
  late final Color borderColor;
  late final bool? select;
  late final bool? enable;
  late final double? fontSize;
  late final int line;
  late final kButtonStyle style;
  final void Function(String?)? onClick;
  late final Widget? child;

  ///构造方法，默认是文本模式
  kButton(
      {super.key,
      required this.name,
      this.style = kButtonStyle.text,
      this.textColor = Colors.black,
      this.groundColor = Color_bg_view,
      this.borderColor = Colors.black,
      this.line = 1,
      this.onClick,
      this.select,
      this.enable,
      this.fontSize,
      this.child});

  @override
  State<kButton> createState() => kButtonState();
}

class kButtonState<T extends kButton> extends State<T> {
  bool _isSelected = false;

  bool get selected => _isSelected;
  set selected(bool isSelected) {
    setState(() {
      _isSelected = isSelected;
    });
  }

  Widget? _child() {
    switch (widget.style) {
      case kButtonStyle.text:
        return Text(
          maxLines: widget.line,
          widget.name,
          textAlign: TextAlign.center,
          style: TextStyle(
              letterSpacing: 0,
              fontSize: widget.fontSize ?? 16,
              color: widget.textColor), // 你可以设置初始的字体大小
        );
      case kButtonStyle.icon:
        return Image.asset(
          widget.name,
          fit: BoxFit.cover, // 让图片铺满按钮
          width: 30,
          height: 30,
        );
      case kButtonStyle.custom:
        return widget.child;
      default:
        return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: widget.enable != null && widget.enable == false
          ? null
          : () {
              if (widget.onClick != null) {
                switch (widget.style) {
                  case kButtonStyle.text:
                    widget.onClick!(widget.name);
                  case kButtonStyle.icon:
                    widget.onClick!(null);
                  case kButtonStyle.custom:
                    widget.onClick!(null);
                  default:
                    break;
                }
              }
            },
      child: widget.line > 1 && widget.style == kButtonStyle.text
          ? _child()
          : FittedBox(
              fit: BoxFit.fill,
              child: _child(),
            ),
      style: ButtonStyle(
        padding: const WidgetStatePropertyAll(
            EdgeInsets.only(left: 3, right: 3, top: 0, bottom: 0)),
        // 设置最小高度为30像素
        minimumSize: const WidgetStatePropertyAll(
          Size(60, 40),
        ),
        overlayColor: WidgetStateProperty.all(Color_theme.withOpacity(0.05)),
        backgroundColor: WidgetStateProperty.resolveWith<Color?>(
          (Set<WidgetState> states) {
            return widget.groundColor;
          },
        ),
        shadowColor: const WidgetStatePropertyAll(Color_border),
        elevation: const WidgetStatePropertyAll(3.0),
        side: WidgetStatePropertyAll(
            BorderSide(color: widget.borderColor, width: 0.5)),
        shape: WidgetStatePropertyAll(
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(4))),
      ),
    );
  }
}

///自定义纯文本button
class kTextButton extends kButton {
  ///构造方法，默认是文本模式
  kTextButton(
      {super.key,
      required super.name,
      super.style = kButtonStyle.text,
      super.textColor = Colors.black,
      super.groundColor = Color_bg_view,
      super.borderColor = Colors.black,
      super.line = 1,
      super.onClick,
      super.select,
      super.enable,
      super.fontSize})
      : super();
  @override
  State<kTextButton> createState() => kButtonState();
}

///纯图片button
class kIconButton extends kButton {
  kIconButton(
      {super.key,
      required super.name,
      super.style = kButtonStyle.icon,
      super.textColor = Colors.white,
      super.groundColor = Color_bg_view,
      super.borderColor = Colors.black,
      super.line = 1,
      super.onClick,
      super.select,
      super.enable,
      super.fontSize})
      : super();
  @override
  State<kIconButton> createState() => kButtonState();
}

///纯icon button
class imgButton extends StatefulWidget {
  Image img;
  double borderWidth;
  Color overColor;
  Color groundColor;
  double shadowRadius;
  bool? enable;
  final void Function()? onClick;

  imgButton(
      {super.key,
      required this.img,
      required this.onClick,
      this.borderWidth = 0.5,
      this.overColor = Colors.grey,
      this.shadowRadius = 3,
      this.enable,
      this.groundColor = Colors.transparent});

  @override
  State<imgButton> createState() => _ImgButtonState();
}

class _ImgButtonState extends State<imgButton> {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: widget.enable != null && widget.enable == false
          ? null
          : () {
              if (widget.onClick != null) {
                widget.onClick!();
              }
            },
      child: widget.img,
      style: ButtonStyle(
        padding: const WidgetStatePropertyAll(EdgeInsets.zero),
        // 设置最小高度为30像素
        minimumSize: const WidgetStatePropertyAll(
          Size(60, 40),
        ),
        overlayColor: WidgetStateProperty.all(widget.overColor),
        backgroundColor:
            WidgetStateProperty.resolveWith<Color?>((Set<WidgetState> states) {
          return widget.groundColor;
        }),
        shadowColor: const WidgetStatePropertyAll(Color_border),
        elevation: WidgetStatePropertyAll(widget.shadowRadius),
        shape: WidgetStatePropertyAll(
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(4))),
      ),
    );
  }
}
